// PIC18F14K50 int temp18
// Laungage : MPLAB C18



#include <p18f14k50.h>
#include <delays.h>
#include "lib_adc.h"



#pragma config CPUDIV=NOCLKDIV //CPUデバイダ÷1
#pragma config USBDIV = OFF //USBデバイダ÷1
#pragma config FOSC = IRC //内部クロック使用
#pragma config PLLEN = ON //PLL×4
#pragma config PCLKEN = OFF
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF,BOREN = OFF
#pragma config WDTEN = OFF
#pragma config MCLRE = OFF
#pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF
#pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF
#pragma config BORV = 30
#pragma config WDTPS = 32768

#define lcd_DB LATB
#define lcd_E LATCbits.LATC3
#define lcd_RS LATCbits.LATC4

#define Delay_50uS Delay10TCYx(5)
#define Delay_1mS Delay1KTCYx(1)
#define Delay_2mS Delay1KTCYx(2)
#define Delay_5mS Delay1KTCYx(5)
#define Delay_20mS Delay1KTCYx(20)


void SWchange(void);
#pragma interrupt SWchange
#pragma code SWvect = 0x0008
void SW(void){
_asm goto SWchange _endasm
}
#pragma code

unsigned char sw;

void SWchange(){
if(INTCONbits.INT0IF){
INTCONbits.INT0IF = 0;
if(sw == 0) {
sw = 1;
}else{
sw = 0;
}
}
}

void lcd_w4(char asci);
void lcd_c4(char cmd);
void lcd_clr(void);
void lcd_init(void);
void lcd_puts(char *s);
void lcd_locate(char y,char x);


void lcd_w4(char c){
lcd_DB = (c & 0xF0) | (lcd_DB & 0x0F);
lcd_E = 1;
Delay1TCY();
lcd_E = 0;
}

void lcd_c4(char c){
lcd_RS = 0;
lcd_w4(c);
lcd_w4(c<<4);
if(c & 0xFC)
Delay_50uS;
else
Delay_2mS;
}

void lcd_clr(void){
lcd_c4(0x01);
}

void lcd_locate(char y, char x){
unsigned char p;
switch(y & 0x03){
case 0: p=0x80; break;
case 1: p=0xC0; break;
case 2: p=0x94; break;
case 3: p=0xD4; break;
}
lcd_c4(p += x);
}

void lcd_putchr(char c){
lcd_RS = 1;
lcd_w4(c);
lcd_w4(c<<4);
Delay_50uS;
}

void lcd_puts(char *s){
while(*s)
lcd_putchr(*s++);
}

void lcd_putv(int v){
char i;
char buf[5];
v *=49;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(v%10)+'0';
v=v/10;
i--;
} while(v>0);
lcd_putchr(buf[0]);
lcd_putchr('.');
for(i=1;i<4;i++)
lcd_putchr(buf[i]);
}

void lcd_putt(int t){
char i;
char buf[5];
t *=49;
for(i=0;i<5;i++)
buf[i]='0';
i=4;
do{
buf[i]=(t%10)+'0';
t=t/10;
i--;
} while(t>0);
lcd_putchr(buf[1]);
lcd_putchr(buf[2]);
lcd_putchr('.');
lcd_putchr(buf[3]);
}


void lcd_init(void){
Delay_20mS;
lcd_RS = 0;
lcd_w4(0x30);Delay_5mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x30);Delay_1mS;
lcd_w4(0x20);Delay_1mS;
lcd_c4(0x2E);
lcd_c4(0x08);
lcd_c4(0x0C);
lcd_c4(0x06);
lcd_c4(0x01);
}

char unit[] = "(V)";
char ch1[] = "ch1 ";
char ch2[] = "ch2 ";
char temp[] = "temp ";
char unitc[] = "(C)";
char blank[] = " ";


void main(void){
int v,v0,v1, t,t0;

OSCCON = 0b01010010;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
TRISA = 0b00000000;
TRISB = 0b00000000;
TRISC = 0b11000101;
ANSEL = 0b01000000;
ANSELH = 0b00000011;

RCONbits.IPEN = 0;
INTCONbits.INT0IE = 1;
INTCONbits.INT0IF = 0;
INTCONbits.GIE = 1;

lcd_init();
sw = 0;

while(1){
Delay_20mS;

if(sw == 0){
v0 = 1;
v1 = 1;

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH8 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
v=(ADRESH*256) + ADRESL;

v0=v;
lcd_locate(0,2);
lcd_puts(ch1);
lcd_putv(v0);
lcd_puts(unit);

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH9 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
v=(ADRESH*256) + ADRESL;

v1=v;
lcd_locate(1,2);
lcd_puts(ch2);
lcd_putv(v1);
lcd_puts(unit);

}else{

OpenADC(ADC_FOSC_4 &
ADC_RIGHT_JUST &
ADC_12_TAD,
ADC_REF_VDD_VDD &
ADC_REF_VDD_VSS,
ADC_CH6 &
ADC_INT_OFF );

ConvertADC();
while(BusyADC());
t=(ADRESH*256) + ADRESL;

t0=t;
lcd_locate(0,2);
lcd_puts(temp);
lcd_putt(t0);
lcd_puts(unitc);
lcd_locate(1,2);
lcd_puts(blank);
}
}
}


                      戻る